//
//  UITextField+Extension.swift
//  ZYA
//
//  Created by WB Zhou on 2017/4/11.
//  Copyright © 2017年 aowang. All rights reserved.
//

import Foundation
import UIKit
 
typealias TextViewEditAction = (UITextView)->()
extension UITextView:UITextViewDelegate {
    private struct AssociatetapdKeys{
        static var actionKey = "actiontapKey"
        static var actionEditEndKey = "actionEditEndKey"
    }
    @objc dynamic var action: TextViewEditAction? {
        set{
            objc_setAssociatedObject(self,&AssociatetapdKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let action = objc_getAssociatedObject(self, &AssociatetapdKeys.actionKey) as? TextViewEditAction {
                return action
            }
            return nil
        }
    }
    @objc dynamic var actionEditEnd: TextViewEditAction? {
        set{
            objc_setAssociatedObject(self,&AssociatetapdKeys.actionEditEndKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let action = objc_getAssociatedObject(self, &AssociatetapdKeys.actionEditEndKey) as? TextViewEditAction {
                return action
            }
            return nil
        }
    }
    func addTextEndEditClosure(_ action:@escaping  TextViewEditAction) {
        self.actionEditEnd = action
        self.delegate = self
     }
    func addTextChangeClosure(_ action:@escaping  TextViewEditAction) {
        self.action = action
        self.delegate = self
     }
    public func textViewDidEndEditing(_ textView: UITextView) {
        if let action = self.actionEditEnd {
            action(textView)
        }
    }
    public func textViewDidChange(_ textView: UITextView) {
        if let action = self.action {
            action(textView)
        }
     }
}

extension UITextView {
    // label加下划线
     func underline() {
          if let textString = self.text {
              let attributedString = NSMutableAttributedString(string: textString)
              attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
              attributedText = attributedString
          }
      }
    func attributedText(completeString:String,colorText:[(String,UIColor)] = [],fontText:[(String,UIFont)] = []) {
         let tt = NSMutableAttributedString(string: completeString)
        tt.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 16), range: (completeString as NSString).range(of: completeString))
        for item in colorText {
            tt.addAttribute(NSAttributedString.Key.foregroundColor, value: item.1, range: (completeString as NSString).range(of: item.0))
        }
        for item in fontText {
            tt.addAttribute(NSAttributedString.Key.font, value: item.1, range: (completeString as NSString).range(of: item.0))
        }
      self.attributedText = tt
 
    }
}




// 带placeholder
class UITextView_P:UITextView {
    
    var textChanged: ((String?) -> Void)?
    
    fileprivate lazy var placeHolderLabel: UILabel = {
        $0.font = UIFont.systemFont(ofSize: 12)
        $0.textAlignment = .right
        $0.text = "请输入内容~"
        $0.textColor = UIColor.lightGray
        $0.numberOfLines = 0
        return $0
    }(UILabel())
    
     var placeHolder: String? {
        didSet {
            
            placeHolderLabel.text = placeHolder
  
        }
    }
    var plaAlignment:NSTextAlignment? {
        didSet {
            if plaAlignment != nil {
                // 让在属性哪里修改的字体,赋给给我们占位label
                placeHolderLabel.textAlignment = plaAlignment!
            }
        }
    }
    override var font: UIFont? {
        didSet {
            if font != nil {
                // 让在属性哪里修改的字体,赋给给我们占位label
                placeHolderLabel.font = font
            }
        }
    }
    
    // 重写text
    override var text: String? {
        didSet {
            // 根据文本是否有内容而显示占位label
            placeHolderLabel.isHidden = hasText
        }
    }
    
    // frame
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    // xib
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
    // 添加控件,设置约束
    fileprivate func setupUI() {
         // 监听内容的通知
        NotificationCenter.default.addObserver(self, selector: #selector(UITextView_P.valueChange), name: UITextView.textDidChangeNotification, object: nil)
        
        // 添加控件
        addSubview(placeHolderLabel)
  
        // 设置约束,使用系统的约束
        placeHolderLabel.translatesAutoresizingMaskIntoConstraints = false

        addConstraint(NSLayoutConstraint(item: placeHolderLabel, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: -10))
    }
    
    // 内容改变的通知方法
    @objc fileprivate func valueChange() {
        //占位文字的显示与隐藏
        placeHolderLabel.isHidden = hasText
        
        textChanged?(self.text)
    }
    // 移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // 子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置占位文字的坐标
        placeHolderLabel.frame.origin.x = 5
        placeHolderLabel.frame.origin.y = 7
  
    }
}
